package net.lab1024.sa.admin.module.business.file.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.file.dao.DocmentbDao;
import net.lab1024.sa.admin.module.business.file.domain.entity.DocmentbEntity;
import net.lab1024.sa.admin.module.business.file.domain.form.add.DocmentbAddForm;
import net.lab1024.sa.admin.module.business.file.domain.form.query.DocmentbQueryForm;
import net.lab1024.sa.admin.module.business.file.domain.form.update.DocmentbUpdateForm;
import net.lab1024.sa.admin.module.business.file.domain.vo.DocmentbVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.admin.module.system.employee.service.EmployeeService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-07-08 15:14:54
 * @Copyright AKITA
 */

@Service
public class DocmentbService {

    @Resource
    private DocmentbDao docmentbDao;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DocmentService docmentService;

    /**
     * 分页查询
     */
    public PageResult<DocmentbVO> queryPage(DocmentbQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<DocmentbVO> list = docmentbDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(DocmentbAddForm addForm) {
        DocmentbEntity docmentbEntity = SmartBeanUtil.copy(addForm, DocmentbEntity.class);
        docmentbDao.insert(docmentbEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(DocmentbUpdateForm updateForm) {
        DocmentbEntity docmentbEntity = SmartBeanUtil.copy(updateForm, DocmentbEntity.class);
        docmentbDao.updateById(docmentbEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        docmentbDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry, Integer lineNum) {
        docmentbDao.delete(new LambdaQueryWrapper<DocmentbEntity>().eq(DocmentbEntity::getDocEntry, docEntry).eq(DocmentbEntity::getLineNum, lineNum));
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> batchAdd(DocmentbAddForm docmentbAddForm) {
        docmentService.chackVersion(docmentbAddForm.getDocEntry(), docmentbAddForm.getBaseVersion());
        List<EmployeeEntity> employeeEntities = employeeDao.selectBatchIds(Arrays.asList(docmentbAddForm.getIds()));
        int i = docmentbDao.selectMaxLineNum(docmentbAddForm.getDocEntry());
        DocmentbEntity docmentbEntity = new DocmentbEntity();
        docmentbEntity.setDocEntry(docmentbAddForm.getDocEntry());
        for (EmployeeEntity employeeEntity : employeeEntities) {
            docmentbEntity.setLineNum(++i);
            docmentbEntity.setLineStatus("O");
            docmentbEntity.setUserId(employeeEntity.getEmployeeId());
            docmentbEntity.setUserName(employeeEntity.getActualName());
            docmentbDao.insert(docmentbEntity);
        }
        docmentService.updateVersion(docmentbAddForm.getDocEntry(), docmentbAddForm.getBaseVersion());
        return ResponseDTO.ok();

    }

    public ResponseDTO<String> deleteList(Map<String, Object> map) {
       String docEntry = String.valueOf(map.get("docEntry"));
       List<Integer> lineList= (List<Integer>) map.get("lineNums");
        docmentbDao.delete(new LambdaQueryWrapper<DocmentbEntity>().eq(DocmentbEntity::getDocEntry, docEntry).in(DocmentbEntity::getLineNum, lineList));
        return ResponseDTO.ok();
    }
}
